package com.dehui.traders.ui.market.platedetail;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.AdapterView;

import com.dehui.traders.BR;
import com.dehui.traders.R;
import com.dehui.traders.bean.PlateListBean;
import com.dehui.traders.databinding.ActivityPlateDetailBinding;
import com.dehui.traders.ui.base.AppActvity;
import com.dehui.traders.ui.market.rankdetail.RankDetailActivity;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.List;

public class PlateDetailActivity extends AppActvity<ActivityPlateDetailBinding,PlateDetailViewModel>{

    public static final String IntentKey_TypeCode = "IntentKey_TypeCode";
    public static final String IntentKey_Name = "IntentKey_Name";
    private SmartRefreshLayout refreshLayout;
    private String typeCode;
    private String name;
    private RecyclerView recyclerView;
    private PlateDetailAdapter adapter;
    private List<PlateListBean.PlateData> list;

    @Override
    public int initContentView() {
        return R.layout.activity_plate_detail;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    public static void start(Context context,String typeCode,String name){
        Intent intent = new Intent(context,PlateDetailActivity.class);
        intent.putExtra(IntentKey_TypeCode,typeCode);
        intent.putExtra(IntentKey_Name,name);
        context.startActivity(intent);
    }

    @Override
    public PlateDetailViewModel initViewModel() {
        return new PlateDetailViewModel(this);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        typeCode = getIntent().getStringExtra(IntentKey_TypeCode);
        name = getIntent().getStringExtra(IntentKey_Name);
        initToolbar("市场", name);
        recyclerView = findViewById(R.id.recycler_view);
        refreshLayout = findViewById(R.id.sr_refresh);
        refreshLayout.setRefreshHeader(new ClassicsHeader(this));
        viewModel.tradeupdownlist(typeCode,0);

        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new PlateDetailAdapter();
        recyclerView.setAdapter(adapter);

        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                viewModel.tradeupdownlist(typeCode,0);
            }
        });
        adapter.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                PlateListBean.PlateData plateData = list.get(position);
                RankDetailActivity.start(PlateDetailActivity.this,plateData.id,plateData.title);
            }
        });
    }

    public void updataList(List<PlateListBean.PlateData> list) {
        this.list = list;
        refreshLayout.finishRefresh();
        adapter.refresh(list);
    }
}
